<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page trimDirectiveWhitespaces="true"%>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.SQLException" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page language="java" import="java.lang.*" %>
<%@ page import = "java.sql.SQLException" %>
<%@ page import = "com.seniorproject.aims.*" %>
<%@ page import = "java.util.List" %>
<%@ page  import="java.util.ArrayList"%>
<%@ page import = "java.util.Properties" %>
<%@ page import = "javax.servlet.ServletContext" %>
<%@ page import = "java.io.File" %>
<%@ page import = "java.io.InputStream" %>
<%@ page import = "java.util.Date" %>
<%@ page import = "java.text.SimpleDateFormat" %>
<%@page import="com.seniorproject.aims.PasswordHandler"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Student File Detail</title>
<link rel="stylesheet" type="text/css" href="css/blue_button.css" />
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">

<link rel="stylesheet" type="text/css" href="css/stylev2.css" />

<script type="text/javascript" src="js/jquery-2.1.1.min.js"></script>
 <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
 <script type="text/javascript" src="js/bootbox.js"></script>




<script>

$(document).ready(function(){
	
	  var xmlhttp;

		var file_path ;
		var fileNameOld;
		var student_id;
		file_path = document.getElementById("file_path_message").value;
		fileNameOld = document.getElementById("fileNameOld").value;
		student_id = document.getElementById("student_id").value;
		alert(fileNameOld);
		
		if(window.XMLHttpRequest) {
			// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttp = new XMLHttpRequest();
		}
		else {
			// code for IE6, IE5
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		
		xmlhttp.onreadystatechange = function() {
			if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {	
				//alert("I'm Back");//////////////////
				
				var result = xmlhttp.responseText;
				alert("result:"+result);
				if(result=="fail")
				{
					alert("ไม่สามารถทำการ Import ไฟล์ดังกล่าวได้ กรุณาตรวจสอบรูปแบบไฟล์นำเข้า");
				}
				else{
			    	document.getElementById("progress_bar").style.width="100%";
					var x =  document.getElementById("progress_bar") ; 
						x.innerHTML ="100%";
					
				
				}
				
				
				
			
			}// end if check state
		}// end function
		
		//alert("Before send :" + file_path);
		xmlhttp.open("POST", "student_import_file_all_background.jsp?file_path="+file_path+"&file_name_old="+fileNameOld+"&student_id="+student_id, true);
		xmlhttp.send();

	
	
});

function SendBack(){

	var stu_id = document.getElementById("student_id").value;
	var form = document.createElement("form");
	    form.setAttribute("method", "post");
	    form.setAttribute("action", "student_file_all.jsp");
	    var hiddenField = document.createElement("input");
	    hiddenField.setAttribute("type", "hidden");
     hiddenField.setAttribute("name", "student_id");
     hiddenField.setAttribute("id","student_id");
     hiddenField.setAttribute("value", stu_id);
     form.appendChild(hiddenField);
     
     document.body.appendChild(form);
     form.submit();

}

</script>

</head>
<body>

<%
	
	// get session
	String strUser = String.valueOf(session.getAttribute("sUser"));
	HttpSession htp_session = request.getSession();
	
	
	// check session
	if (htp_session == null || htp_session.getAttribute("sUser") == null) {
	    // Forward the control to login.jsp if authentication fails or session expires
	    request.getRequestDispatcher("/login.jsp").forward(request,
	        response);
	}
	
	if("admin".equals(htp_session.getAttribute("sUser"))){%>
	<script>alert("This user dont have PERMISSION to access this zone.");</script>
	<meta HTTP-EQUIV="Refresh" CONTENT	="0; URL=admin_home.jsp"> 	
	<%}
	
	
	
	//////////get permission ///////////
	
	String name = String.valueOf(session.getAttribute("name"));
	String surname = String.valueOf(session.getAttribute("surname"));
	String role_name = String.valueOf(session.getAttribute("student_role"));
	
	String department_id = String.valueOf(session.getAttribute("department_id"));
	
	
	System.out.println("name : " + name);///////////////////////////////
	System.out.println("department_id : " + department_id);/////////////////////////
	
	
	
	List<String> system_name = new ArrayList<String>();
	
	Permission permission = new Permission(strUser,name);
	permission.setDepartmentId(department_id);
	permission.setStudentCreate(Boolean.valueOf(String.valueOf(session.getAttribute("student_create"))));
	permission.setStudentDelete(Boolean.valueOf(String.valueOf(session.getAttribute("student_delete"))));
	permission.setStudentRead(Boolean.valueOf(String.valueOf(session.getAttribute("student_read"))));
	permission.setStudentUpdate(Boolean.valueOf(String.valueOf(session.getAttribute("student_update"))));
	
	String permiss = "";
	if(permission.getStudentCreate()) {
		permiss = permiss + "Add";
	}
	if(permission.getStudentUpdate()) {
		if("".equals(permiss)) {
			permiss = permiss + "Edit";
		} else {
			permiss = permiss + " Edit";
		}
	}
	if(permission.getStudentRead()) {
		if("".equals(permiss)) {
			permiss = permiss + "Read";
		} else {
			permiss = permiss + " Read";
		}
	}
	if(permission.getStudentDelete()) {
		if("".equals(permiss)) {
			permiss = permiss + "Delete";
		} else {
			permiss = permiss + " Delete";
		}
	}
	//check permission
	
	if(!permission.getStudentRead()){%>
		<script>alert("This user dont have PERMISSION to access this zone.");</script>
		<meta HTTP-EQUIV="Refresh" CONTENT="0; URL=student_home.jsp"> 	
	<%}
	
	/*
	System.out.println("Reset message_path");
	session.setAttribute("message_path", "0");
	System.out.println("message_path:"+session.getAttribute("message_path"));
	*/
	
	//set Database Connection
	String hostProps = "";
	String usernameProps  = "";
	String passwordProps  = "";
	
	try {
		//get current path
		ServletContext servletContext = request.getSession().getServletContext();
		
		InputStream input = servletContext.getResourceAsStream("/properties/connectDB.properties");
		Properties props = new Properties();
		
		props.load(input);
	
		hostProps  = props.getProperty("host");
		usernameProps  = props.getProperty("username");
		passwordProps  = props.getProperty("password");
	} catch (Exception e) { 
		out.println(e);  
	}
	
	System.out.println("hostProperties : "+hostProps);////////////////try/////////////////
	System.out.println("usernameProperties : "+usernameProps);////////////////try/////////////////
	System.out.println("passwordProperties : "+passwordProps);////////////////try/////////////////
	
	// connect database
	Connection connect = null;		
	try {
		Class.forName("com.mysql.jdbc.Driver");
	
		connect =  DriverManager.getConnection("jdbc:mysql://" + hostProps  + "/AIMS" +
				"?user=" + usernameProps  + "&password=" + passwordProps );
	
		if(connect != null){
			System.out.println("Database Connect Sucesses.");
		} else {
			System.out.println("Database Connect Failed.");	
		}
	
	} catch (Exception e) {
		out.println(e.getMessage());
		e.printStackTrace();
	}
	///////////////////// ////////////////////////////////
	System.out.println("@Student_file_all_import_result");
	String fileNameOld = new String(request.getAttribute("fileNameOld").toString().getBytes("ISO8859_1"),"UTF-8");
	
	System.out.println("FileNameOld:"+fileNameOld);
	

	connect.close();

%>

<div id="page-wrap">
		<div id="inside">
			<table border="0">
				<tr style="font-size:24px">
					<td width="600" height="100" style= "background-color: #273b80;border: 0px ;padding: 0px; 0px;" >
						<div id="header_banner-2"></div>
					</td>
					
					<td width="680" height="100" style= "background-color: #273b80;border: 0px;padding: 0px; 0px;" >
						<p align="right"><label><font size="4" color="#ffffff">username : <%=strUser%>&nbsp;&nbsp;</font></label></p>
						<a href="logout.jsp"><p align="right"><label><font size="3" color="#ffffff">log out</font></label></p></a>
					</td>
				</tr>
			</table>

				
				<div id="main-content-ac">
					
					<div class="container">
					<center>
  						<h2>Import In Progress</h2>
  					</center>	
					 <div class="progress">
					    <div id="progress_bar" class="progress-bar progress-bar-striped active" role="progressbar" aria-valuenow="50" aria-valuemin="0" aria-valuemax="100" style="width:50%">
					      50%
					    </div>
					  </div>
					</div>
					
					  <input type="hidden" id="file_path_message" value="<%= request.getAttribute("message") %>">
					  <input type="hidden" id="fileNameOld" value="<%=fileNameOld %>">
					  <input type="hidden" id="student_id" value="<%=request.getAttribute("student_id") %>">
					   <br><br><br><br><br><br><br>
						
						
						<center>
					  			<button class="myButton" onClick="SendBack();">ย้อนกลับ</button>
					  	</center>
					  
					  <br><br><br><br><br>
					
				
				
			<div style="clear: both;"></div>
			
			
				
			<div id="footer"></div>
			
		</div>		
			<div style="clear: both;"></div>		
   </div>


</body>
</html>